package com.by.controller;

import com.by.bean.PageBean;
import com.by.bean.ProductInfo;
import com.by.bean.ResultBean;
import com.by.query.ProductInfoQuery;
import com.by.service.ProductInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * date 2025/2/20 17:54
 * author bjj
 * decoration
 * version 1.0
 * since jdk11
 */
@RestController
@Api(tags = "产品管理")
@RequestMapping("/api")
public class ProductInfoController extends BaseController {
    @Autowired
    ProductInfoService productInfoService;

    /**
     * 根据条件查询产品列表
     *
     * @param productInfoQuery
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", required = true)}
    )
    @ApiOperation("根据条件查询产品列表")
    @PostMapping("/productInfo/query")
    public ResultBean query(@RequestBody ProductInfoQuery productInfoQuery) {
        PageBean pageBean = productInfoService.query(productInfoQuery);
        return success("查询成功", pageBean);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "productName", value = "产品名称", required = true),
            @ApiImplicitParam(name = "productType", value = "产品类型", required = true),
            @ApiImplicitParam(name = "price", value = "产品价格", required = true),
            @ApiImplicitParam(name = "productCategory", value = "产品分类", required = true),
            @ApiImplicitParam(name = "model", value = "型号", required = true),
    })
    @ApiOperation("保存产品信息")
    @PostMapping("/productInfo/save")
    public ResultBean save(@RequestBody ProductInfo productInfo) {
        int rs = productInfoService.save(productInfo);
        return saveResult(rs);
    }

    @ApiImplicitParam(name = "productId", value = "产品id", required = true)
    @ApiOperation("更新产品信息")
    @PostMapping("/productInfo/update")
    public ResultBean update(@RequestBody ProductInfo productInfo) {
        int rs = productInfoService.update(productInfo);
        return updateResult(rs);
    }

    @ApiOperation("删除产品信息")
    @DeleteMapping("/productInfo/delete")
    public ResultBean delete(@RequestParam String id) {
        int rs = productInfoService.delete(id);
        return deleteResult(rs);
    }

    @ApiOperation("根据id查询产品信息")
    @GetMapping("/productInfo/getById")
    public ResultBean getById(@RequestParam String id) {
        ProductInfo productInfo = productInfoService.get(id);
        if (productInfo == null) {
            return fail("产品信息不存在");
        }
        return success("查询成功", productInfo);
    }

}
